<?php

namespace App\Http\Controllers\Admin;

use App\Models\Article;
use App\Models\ArticleTag;
use App\Models\Tag;
use Illuminate\Http\Request;
use App\Http\Requests\article\store;
use App\Http\Controllers\Controller;
use App\Models\Category;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //获取文章列表
        $articles=Article::select(
            'id', 'category_id', 'title',
            'author', 'description', 'cover',
            'is_top', 'created_at','click','keywords',"deleted_at"
        )
            ->orderBy('created_at', 'desc')
//            ->with(['category', 'tags'])
            ->paginate(5);
//        dd($articles);
        return view("admin.article.index",compact('articles'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //发布新文章
        //获取所有标签
        $tags=Tag::get();
        //获取所有分类
        $cats=Category::select("id","name")->get();
        return view('admin.article.create',compact('tags','cats'));
    }

    /**
     * 添加文章
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Store $store,Article $articleModel)
    {
//        var_dump($store->all());
//        exit;
//        dd($store->all());
        $data=$store->except("_token");
        if($store->hasFile('cover')){
            $res=uploadFile("cover","upload/cover",array("jpg","jpeg","png","gif"));
            if($res['status_code']==200){
                $data['cover']=$res['url'];
            }
        }
        $articleModel->storeData($data);
        return redirect('admin/article/index');
    }

    public function uploadImage(Request $request){
        $returnArr=[
            "success"=>0,
            "message"=>"未上传文件",
        ];
        if($request->hasFile('editormd-image-file')){
            $res=uploadFile("editormd-image-file","upload/article",array("jpg","jpeg","png","gif"));
            if($res['status_code']==200){
                $returnArr['success']=1;
                $returnArr['url']=$res['url'];
            }
            $returnArr['message']=$res['message'];
        }
        return response()->json($returnArr);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //编辑文章
        //获取所有标签
        $tags=Tag::get();
        //获取所有分类
        $cats=Category::select("id","name")->get();
        //获取文章已具有的标签
        $old_tags=ArticleTag::where("article_id",$id)->pluck('tag_id')->toArray();
//        var_dump($old_tags);
        $article = Article::withTrashed()->find($id);
        return view('admin/article/edit',compact('article','tags','old_tags',"cats"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Article::destroy($id);
        return redirect("admin/article/index");
    }
}
